package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.OwnerCarriageDetail;
import com.sgb.kjwl.model.entity.transport.VehicleDetail;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.view.entity.PermissionEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.transport.CarriageDetailActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/1/11
 * Author:黄淋
 * Description:
 */
public class CarriageDetailModel extends AndroidViewModel {
    public MutableLiveData<VehicleDetail> ldCarDetail = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldInquiry = new MutableLiveData<>();//询价权限

    public CarriageDetailModel(@NonNull Application application) {
        super(application);
    }

    public void getDetailData(CarriageDetailActivity mActivity, String no) {

        DialogHelper.showProgressDialog(mActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getCarriageDetail(no).enqueue(new Callback<BaseEntity<OwnerCarriageDetail>>() {
            @Override
            public void onResponse(Call<BaseEntity<OwnerCarriageDetail>> call, Response<BaseEntity<OwnerCarriageDetail>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null || response.body().getState() == null || response.body().getData() == null)
                    return;

                if (response.body().getState().equals("ok")) {

                    mActivity.setDetail(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<OwnerCarriageDetail>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void refresh(CarriageDetailActivity mActivity) {
        //获取用户数据
        //TODO 接口有时候报错，改为不提示报错内容 Callback
        NewAPIManager.getInstance().getLogisticsAPI().getLogisticsPermissions().enqueue(new ICallBack<BaseEntity<PermissionEntity>>() {
            @Override
            public void onSuccess(BaseEntity<PermissionEntity> data) {
                if(data.isSuccess()){
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, data.getData().getUserType());
                    UserOperating.getInstance().setUserInfo(data.getData());
                    mActivity.setUserInfo();
                }
            }
        });
    }

    /**
     * 获取车辆详情
     * @param driverAuthNo
     */
    public void loadData(String driverAuthNo) {
        NewAPIManager.getInstance().getLogisticsAPI().getFindCarVehicleDetail(driverAuthNo).enqueue(new Callback<BaseEntity<VehicleDetail>>() {
            @Override
            public void onResponse(Call<BaseEntity<VehicleDetail>> call, Response<BaseEntity<VehicleDetail>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    ldCarDetail.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<VehicleDetail>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 查询询价权限
     * @param carriageNo
     */
    public void getInquiryPermissions(String carriageNo) {
        if (!TextUtils.isEmpty(carriageNo)) {
            NewAPIManager.getInstance().getLogisticsAPI().getInquiryPermissions("", carriageNo).enqueue(new ICallBack<BaseEntity<Boolean>>() {
                @Override
                public void onSuccess(BaseEntity<Boolean> data) {
                      if(data.isSuccess()){
                          ldInquiry.postValue(data.getData());
                      }
                }
            });
        }
    }
}
